package com.mrsphoto.mrphoto.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.mrsphoto.mrphoto.R;
import com.mrsphoto.mrphoto.bean.User;
import com.mrsphoto.mrphoto.fragment.FirstFragment;
import com.mrsphoto.mrphoto.model.IuserLoginView;
import com.mrsphoto.mrphoto.presenter.UserLoginPresenter;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppActivity implements IuserLoginView {

    UserLoginPresenter userLoginPresenter = new UserLoginPresenter(this);
    Intent intent = new Intent();
    @Bind(R.id.name)
    EditText name;
    @Bind(R.id.password)
    EditText password;
    @Bind(R.id.ok)
    Button ok;
    @Bind(R.id.cancel)
    Button cancel;
    @Bind(R.id.progressbar)
    ProgressBar progressbar;

    @Override
    protected BaseFragment getFirstFragment() {
        return FirstFragment.newInstance("MainActivity");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }


    @Override
    public String getUserName() {
        return name.getText().toString();
    }

    @Override
    public String getPassword() {
        return password.getText().toString();
    }

    @Override
    public void clearUserName() {
        name.setText("");
    }

    @Override
    public void clearPassword() {
        password.setText("");

    }

    @Override
    public void showLoading() {
        progressbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        progressbar.setVisibility(View.GONE);
    }

    @Override
    public void toMainActivity(User user) {
        Toast.makeText(this, user.getName() +
                " login success , to MainActivity", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void showFailedError() {
        Toast.makeText(this,
                " login error , to MainActivity", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void intentView(final Class cl) {
        intent.setClass(MainActivity.this, cl);
        startActivity(intent);
    }

    @OnClick({R.id.ok, R.id.cancel})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ok:
                userLoginPresenter.login();
                break;
            case R.id.cancel:
                userLoginPresenter.clear();
                break;
        }
    }
}
